<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">

      <el-button
        type="primary"
        @click="handleAdd">添加</el-button>


      <!--列表-->
      <el-table :data="exams" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="name" label="名称" sortable></el-table-column>
        <el-table-column prop="createPerson" label="创建者" sortable></el-table-column>
        <el-table-column prop="UploadDate" label="创建时间" sortable></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <!--<el-table-column prop="age" label="name数量" width="100" sortable>-->
        <!--</el-table-column>-->
      </el-table>


      <el-dialog title="上传试卷" :visible.sync="dialogFormVisible">
        <el-form :model="selectTable">
          <el-form-item label="试卷名称" label-width="100px">
            <el-input v-model="selectTable.name"></el-input>
          </el-form-item>
          <el-form-item label="选择试卷">
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:8001/wzz/upload/"
              :on-success="handleExamSuccess"
            >
              <!--:file-list="fileList"-->
              <!--:on-success="updatefileList"-->

              <i class="el-icon-plus"></i>  <!-- vue warning 是上传列表的问题  -->
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="createExam">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import headTop from './head.vue'
  import { getExamList, createExam, ExamListDel } from '@/api/api'
  export default {
    data () {
      return {
        loading: false,
        filters: {
          name: ''
        },
        selectTable: {
          name: '',
          examURL: ''
        },
        exams: [],
        dialogFormVisible: false
      }
    },
    components: {
      headTop
    },
    created () {
      this.initData()
    },
    methods: {
      // 获取设备名称
      async initData () {
        this.loading = true
        this.selectTable.name = '' // 重置输入框内容
        this.selectTable.examURL = ''
        try {
          getExamList().then((res) => {
            this.exams = res.data  // type 看下转换下，通过store获取
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      handleExamSuccess (res, file) {
//        this.imageUrl = URL.createObjectURL(file.raw)
        this.selectTable.examURL = res.name
      },
      handleAdd () {
        this.dialogFormVisible = true
      },
      async createExam () {
        this.dialogFormVisible = false
        let postData = {
          name: this.selectTable.name,
          filePath: this.selectTable.examURL
        }
        createExam(postData).then(res => {
          if (res.status === 200) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.initData()
          } else {
            this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
          console.log('试卷添加失败', error)
        })
      },
      async handleDelete (index, row) {
        ExamListDel(row.id).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.DNames.splice(index, 1)
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
          console.log('类型删除失败', error)
        })
      },
      async handleEdit (row) {
        this.$router.push({name: '修改试卷', params: {examId: row.id}})
      }
    }
  }

</script>

<style scoped>
  .el-button {
    margin: 5px;
  }
</style>
